/*******************************************************************************
 * SDP Project AUT 2012
 * 
 *                ***********Driving Assistant************
 * Team:
 * Gisela, Deidre, Brijesh, Robert
 * 
 * This class implements the sound files needed for the alert sounds.
 * 
 * @version 1.0
 * 
 * Copyright (c) 2012 by DrivingAssistant Team, Inc., All Rights Reserved.
 * 
 ******************************************************************************/
package com.drivingAssistant.model;

import com.drivingAssistant.gui.DrivingAssistantGUI;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.*;

/**
 *
 * SoundOff class
 * 
 */
public class SoundOff {

    //variables
    File clipFile = null;
    boolean on = true;
    Clip clip = null;
    double ms = 0.0;

    /*
     * empty constructor to initialise SoundOff objects.
     */
    public SoundOff() throws LineUnavailableException {
//        clip = AudioSystem.getClip();
    }

    /**
     * set the sound on
     */
    public void setSoundOn(boolean on) throws LineUnavailableException {
        this.on = on;
    }
    
    public synchronized void stop() {
        this.clip.stop();
    }

    /**
     * set which sound is to played
     * 
     * @param file which is the location of the sound file.
     */
    public synchronized void setClip(String file) {
        this.clipFile = new File(file);
    }

    /**
     * play the appropriate sound file.
     *
     * @param sound which is the string location of the sound file.
     * @throws LineUnavailableException
     */
    public synchronized void playClip(int level) throws LineUnavailableException {
        AudioInputStream ais = null;
        clip = AudioSystem.getClip();
        try {
            ais = AudioSystem.getAudioInputStream(this.clipFile);
            clip.open(ais);
            clip.loop(level);
        } catch (UnsupportedAudioFileException ex) {
            Logger.getLogger(SoundOff.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DrivingAssistantGUI.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (ais != null) {
                    ais.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(DrivingAssistantGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
}
